<template>
    <div class="video-content">
        <div class="right">
            <!-- 主屏幕位置 -->
            <video id="main-screen" src="" width="100%" height="100%"></video>
            <div class="no-video">
                没有视频
            </div>
            <div class="main-screen-nick">
                {{ mainScreenNick }}
            </div>
        </div>
        <div class="left">
            <!-- 右侧屏幕列表 -->
            <div class="list">
                <div class="item" @click="changeScreen(index)" :class="index === currentIndex ? 'acitve' : ''"
                    v-for="(item, index) in userList" :key="index">
                    <video class="item-video" v-if="index % 2 == 0" src="" width="100%" height="100%"></video>
                    <div v-else class="user-ipc">
                        <img src="http://127.0.0.1:7878/1684241760108-test2.jpeg" alt="">
                    </div>
                    <div class="item-nick">{{ item.nick }}</div>
                </div>
            </div>
        </div>
    </div>
</template>

<script setup lang="ts">
import { ref, onMounted, watch, nextTick } from 'vue'
import { useRoute } from 'vue-router'
const $route = useRoute()

onMounted(() => {
    getScreen()
})

let mockStream = ref<MediaStream>()

// 获取屏幕信息
const getScreen = async () => {
    let sourceId = ($route.query.sourceId) as string
    const stream = await navigator.mediaDevices.getUserMedia({
        audio: false,
        video: <any>{
            mandatory: {
                chromeMediaSource: 'desktop',
                chromeMediaSourceId: sourceId,
                maxFrameRate: 120, // 帧率
                minWidth: 1280,
                maxWidth: 1280,
                minHeight: 720,
                maxHeight: 720
            }
        }
    })

    let mainScreen: HTMLVideoElement = (document.querySelector('#main-screen')) as HTMLVideoElement
    mainScreen!.srcObject = stream
    mainScreen!.play()

    // 模拟数据
    mockStream.value = stream

    return stream
}

// 用户列表
let userList = ref([
    {
        nick: '用户昵称1',
        imgUrl: 'http://127.0.0.1:7878/1684241760108-test2.jpeg',
        isOpenAudio: true,
        isOpenVideo: true,
        stream: null
    },
    {
        nick: '用户昵称2',
        imgUrl: 'http://127.0.0.1:7878/1684241760108-test2.jpeg',
        isOpenAudio: false,
        isOpenVideo: false,
        stream: null
    },
    {
        nick: '用户昵称3',
        imgUrl: 'http://127.0.0.1:7878/1684241760108-test2.jpeg',
        isOpenAudio: false,
        isOpenVideo: false,
        stream: null
    },
    {
        nick: '用户昵称4',
        imgUrl: 'http://127.0.0.1:7878/1684241760108-test2.jpeg',
        isOpenAudio: false,
        isOpenVideo: false,
        stream: null
    },
    {
        nick: '用户昵称5',
        imgUrl: 'http://127.0.0.1:7878/1684241760108-test2.jpeg',
        isOpenAudio: false,
        isOpenVideo: false,
        stream: null
    }
])

// 当前主屏幕的用户下标
let currentIndex = ref<number>(0)
// 当前主屏幕的用户昵称
let mainScreenNick = ref<string>('')

watch(() => currentIndex.value, () => {
    nextTick(() => {
        mainScreenNick.value = userList.value[currentIndex.value].nick
    })
}, { immediate: true })

// 切换主屏幕的用户
const changeScreen = (index: number) => {
    currentIndex.value = index
    mainScreenNick.value = userList.value[index].nick
}
</script>

<style scoped lang="scss">
.video-content {
    width: 100%;
    height: 100%;
    display: flex;

    .right {
        flex: 1;
        height: 100%;
        background-color: #292929;
        border: 2px solid #2c2c2c;
        border-radius: 5px;
        box-sizing: border-box;
        overflow: hidden;
        position: relative;

        .main-screen-nick {
            position: absolute;
            bottom: 5px;
            left: 5px;
            color: #fff;
            font-size: 12px;
            text-shadow: 2px 2px 2px rgba(0, 0, 0, .5);
        }
    }

    .left {
        width: 215px;
        height: 100%;
        margin-left: 10px;
        overflow: hidden;

        .list {
            width: 100%;
            height: 100%;
            overflow: auto;

            &::-webkit-scrollbar {
                display: none;
            }

            .item {
                width: 100%;
                height: 120px;
                background-color: #292929;
                margin-bottom: 10px;
                color: #fff;
                border: 2px solid #2c2c2c;
                border-radius: 5px;
                box-sizing: border-box;
                display: flex;
                justify-content: center;
                align-items: center;
                position: relative;

                &:nth-last-of-type(1) {
                    margin: 0;
                }

                .user-ipc {
                    width: 60px;
                    height: 60px;
                    border-radius: 50%;
                    overflow: hidden;

                    img {
                        width: 100%;
                        height: 100%;
                    }
                }

                .item-nick {
                    position: absolute;
                    left: 5px;
                    bottom: 5px;
                    color: #ffff;
                    text-shadow: 2px 2px 2px rgba(0, 0, 0, .5);
                    font-size: 12px;
                    max-width: 100px;
                    display: flex;
                    align-items: flex-end;
                }
            }

            .acitve {
                &::after {
                    content: '';
                    width: 100%;
                    height: 120px;
                    position: absolute;
                    top: 0;
                    left: 0;
                    border: 3px solid #0091ff;
                    border-radius: 5px;
                    z-index: 2;
                }
            }
        }
    }
}
</style>